package com.mx.controller;

import com.mx.pojo.Article;
import com.mx.pojo.ArticleTag;
import com.mx.pojo.Tag;
import com.mx.service.ArticleService;
import com.mx.service.ArticleTagService;
import com.mx.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    ArticleService articleService;

    @Autowired
    TagService tagService;

    @Autowired
    ArticleTagService articleTagService;


    @RequestMapping({"/admin"})
    public String blog(Model model){
        List<Article> articles = articleService.findAll();
        Collections.reverse(articles);
        model.addAttribute("articles",articles);
        return "admin/dashboard";
    }

    @RequestMapping({"/", "/index"})
    public String index(Integer id, Model model) {
        if (id == null || id == 0)
            id = 1;
        ArrayList<Article> result = new ArrayList<>();
        List<Article> articles = articleService.findAll();
        int pageCount = articles.size() / 3 + 1;
        if ((id - 1) * 3 < articles.size())//如果页没有超出
        {
            for (int i = (id - 1) * 3; i < articles.size() && i < (id - 1) * 3 + 3; i++) {
                String content = articles.get(i).getContent();
                if (content.length() >= 300)
                    articles.get(i).setContent(content.substring(0, 99) + "...");
                result.add(articles.get(i));
            }
            articles = result;
        } else {
            articles = null;
        }


        Collections.reverse(articles);

        model.addAttribute("articles", articles);
        model.addAttribute("current", id);
        model.addAttribute("pageCount", pageCount);
        return "index";
    }

    @RequestMapping("/blog")
    public String hello(Integer id, Model model) {
        Article article = articleService.findId(id);

        Parser parser = Parser.builder().build();
        String content = article.getContent();
        Node document = parser.parse(content);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        content = renderer.render(document);
        article.setContent(content);

        model.addAttribute("article", article);
        return "detail";
    }

    @RequestMapping("/time")
    public String time(Model model) {
        List<Article> articles = articleService.findAll();
        Collections.reverse(articles);

        for (Article article : articles) {
            if(article.getContent().length()>150)
            {
                article.setContent(article.getContent().substring(0,150)+"...");
            }
        }

        model.addAttribute("articles", articles);
        return "time";
    }

    @RequestMapping("/sort")
    public String sort(Model model) {
        List<Tag> tags = tagService.findAll();
        model.addAttribute("tags",tags);
        return "sort";
    }


    @RequestMapping("/sortblog")
    public String sortblog(Integer tid, Model model) {
        List<Tag> tags = tagService.findAll();
        model.addAttribute("tags",tags);
        List<ArticleTag> articleTags = articleTagService.findTid(tid);
        ArrayList<Article> articles = new ArrayList<>();
        for (ArticleTag articleTag : articleTags) {
            articles.add(articleService.findId(articleTag.getAid()));
        }
        for (Article article : articles) {
            if(article.getContent().length()>150)
            {
                article.setContent(article.getContent().substring(0,150)+"...");
            }
        }
        model.addAttribute("articles", articles);
        model.addAttribute("check", "true");
        return "sort";
    }


    @RequestMapping("/about")
    public String about(){
        return "about";
    }
}
